<?php
/**
 * @description:
 * @author     : Geomen
 * @time       : 2018/12/19 10:47
 */


namespace App\Http\Controllers;


use App\Component\WeiXin;
use App\Models\FormId;
use Illuminate\Http\Request;

class WeiXinController extends Controller
{
    /**
     * 添加模板表单ID
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     * @throws \Illuminate\Validation\ValidationException
     */
    public function addFormId(Request $request)
    {
        $this->validate($request,[
            'open_id' => 'required',
            'form_id' => 'required'
        ]);
        $open_id = e($request->input('open_id'));
        $form_id = e($request->input('form_id'));
        $expire_time = time() + 604800;
        FormId::query()->create(compact('open_id','form_id','expire_time'));
        FormId::query()->where('open_id',$open_id)->where('expire_time', '<', time())->delete();
        $total = FormId::query()->where('open_id',$open_id)->count();
        return $this->responseSuccess('添加成功',$total);
    }

    public function sendMsg(Request $request)
    {
        $Wx = new WeiXin();
        $res = $Wx->sendTemplateMessage('ocwn-0P78iuJpumIDFQMFE-orQf4','TmH10Cq9Is44K99u9fJB8l6LGn8urta3v_saPX_mHH0','pages/crontab/index',['test','不可用',date('M-d H:i:s')]);
        return response($res);
    }
}